package com.itic.mall.system.categoryjd.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.sys.common.service.OutCommonService;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.exception.definition.ParameterErrorException;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mall.system.categoryjd.persistence.model.CategoryJd;
import com.itic.mall.system.categoryjd.persistence.model.CategoryJdExample;
import com.itic.mall.system.categoryjd.persistence.model.CategoryJdExample.Criteria;
import com.itic.mall.system.categoryjd.service.CategoryJdService;


@Controller
@RequestMapping(value="/system/categoryjd")
public class CategoryJdController
     extends BaseController {
    @Autowired
    private CategoryJdService categoryJdService;

    @Autowired
	protected OutCommonService outCommonService;
    
    //////////////////////////////////////////////////////常量区////////////////////////////////////////////////////////////////
    private static final String MODEL_NAME_CHINA = "商品分类";//中文名
    private static final String MODEL_NAME＿ENGLISH = "categoryjd";//英文名
    private static final String PAGE_COMMON_PATH = "/mall/system/categoryjd/categoryjd";//通用路径
    private static final String COMMON_URL = "/system/categoryjd/";//通用路径


    //////////////////////////////////////////////////////页面跳转区////////////////////////////////////////////////////////////
    /*
     * 主页面
     */
    @RequestMapping(value="/src")
    public String src(Model model){
    	model.addAttribute("name_china", MODEL_NAME_CHINA);
    	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
    	model.addAttribute("common_url", COMMON_URL);
    	return PAGE_COMMON_PATH+"Blank";
    }
    /*
     * 主页面
     */
    @RequestMapping(value="/init")
    public String init(Model model){
    	model.addAttribute("name_china", MODEL_NAME_CHINA);
    	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
    	model.addAttribute("common_url", COMMON_URL);
    	return PAGE_COMMON_PATH+"Main";
    }
    
    /*
     * 添加页面
     */
    @RequestMapping(value="/initAdd")
    public String initAdd(String id,Model modele){
    	if( StringHelper.isNotBlank(id) ){
    		modele.addAttribute("parentId", id);
    	}
    	
    	modele.addAttribute("reorder", MODEL_NAME_CHINA);
    	modele.addAttribute("name_china", MODEL_NAME_CHINA);
    	modele.addAttribute("name_english", MODEL_NAME＿ENGLISH);
    	modele.addAttribute("common_url", COMMON_URL);
    	return PAGE_COMMON_PATH+"Add";
    }
    
    /*
     * 编辑页面
     */
    @RequestMapping(value="/initEdit")
    public String initEdit(String id , Model model){
    	if( StringHelper.isNotBlank(id) ) {
    		CategoryJd temp = categoryJdService.get(id);
    		model.addAttribute("editData",iticJsonMapper.toJson(temp));
    		model.addAttribute("name_china", MODEL_NAME_CHINA);
        	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
        	model.addAttribute("common_url", COMMON_URL);
    	}else {
            throw new ParameterErrorException();
        }
    	return PAGE_COMMON_PATH+"Edit";
    }
    
    /*
     * 查看页面
     */
    @RequestMapping(value="/initView")
    public String initView(String id , Model model){
    	if( StringHelper.isNotBlank(id) ) {
    		
    		CategoryJd temp  = categoryJdService.get(id);
    		model.addAttribute("viewEdit",iticJsonMapper.toJson(temp));
    		model.addAttribute("name_china", MODEL_NAME_CHINA);
        	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
        	model.addAttribute("common_url", COMMON_URL);
    	}else {
            throw new ParameterErrorException();
        }
    	
    	return PAGE_COMMON_PATH+"View";
    }
    
    /*
     * 导入页面
     */
    @RequestMapping(value="/initImp")
    public String initImp(Model model){
    	model.addAttribute("name_china", MODEL_NAME_CHINA);
    	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
    	model.addAttribute("common_url", COMMON_URL);
    	return PAGE_COMMON_PATH+"Imp";
    }
    
    
    //////////////////////////////////////////////////////业务代码区////////////////////////////////////////////////////////////
    
    /**
     * 添加模板管理数据
     * @param android
     * @return
     */
    @RequestMapping(value="/add")
    @ResponseBody
    public Map<String,String> add(CategoryJd temp){
    	
    	 Map<String, String> resultMap = new HashMap<String, String>();
         String result = ResponseCode.SERVER_ERROR.getCode();
         
         result = categoryJdService.add(temp);
         
         resultMap.put(IticConst.RESULT, result);
         return resultMap;
    }
    
    
    /**
     * <p>Title:queryCondition </p>
     * <p>Description:封装查询条件 </p>
     * @param jzdw
     * @param jzdwExample
     * @return
     */
    private CategoryJdExample queryCondition(HttpServletRequest request,CategoryJd temp) {
    	
    	// 参数处理
        CategoryJdExample example = new CategoryJdExample();
        // 按创建时间倒序
        example.setOrderByClause("create_time desc");
        // 按排列号顺序
        example.setOrderByClause("reorder asc");
        
        Criteria criteria = example.createCriteria().andIsEffectiveEqualTo( DBConst.TRUE );
      
        // 查询条件
        if ( null != temp ) {
    	    //模糊查询
            if ( StringHelper.isNotBlank( temp.getName() ) ) {
                criteria.andNameLike("%" + temp.getName() + "%");
            }
        }
        return example;
    }
    
    /**
     * 
     * @param goldType
     * @param request
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(CategoryJd temp, HttpServletRequest request){
        // 声明返回对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        
        CategoryJdExample example = this.queryCondition(request,temp);
        // 封装分页参数
        example = this.handlePageQuery(request, example);
        
        
        // 获取信息总条数
        int total = categoryJdService.count(example);
        
        // 获取数据
        List<CategoryJd> list = categoryJdService.list(example);
        
        //结果集
        // List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
        /* if( list.size() > 0 ){
            //过滤黑名单字段   
        	resultList = ValueHelper.tranList2MapList(list, ignoreProperties);
        	for( CategoryJd tmp : list ){
        		tmp.setName(tmp.getName()+"("+tmp.getCount()+")");
        	}
        }*/
        
        // 数据返回处理
        resultMap.put(IticConst.TOTAL_RECORDS, total);
        resultMap.put(IticConst.DISPLAY_RECORDS, total);
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        resultMap.put(IticConst.DATA, list);
        return resultMap;

    }
    
    /**
     * 编辑保存
     * @param CategoryJd
     * @return
     */
    @RequestMapping(value="/edit")
    @ResponseBody
    public Map<String,String> edit(String delFileIds , CategoryJd temp){
    
    	if( StringHelper.isNotBlank(delFileIds) ){
    		// 删除附件
    		outCommonService.deleteFiles(delFileIds);
    	}
    	
    	 Map<String, String> resultMap = new HashMap<String, String>();
         String result = ResponseCode.SERVER_ERROR.getCode();
         result = categoryJdService.edit(temp);
         
         resultMap.put(IticConst.RESULT, result);
         return resultMap;
    }
    
    

    /**
     * 删除
     * 
     */
    @RequestMapping(value = "/del")
    @ResponseBody
    public Map<String, String> del(String id) {
        Map<String, String> resultMap = new HashMap<String, String>();
        String result = ResponseCode.SERVER_ERROR.getCode();

        //批量删除
        /*if (StringHelper.isNotBlank(ids)) {
        	
            List<String> idList = Arrays.asList(ids.split(","));
            
            CategoryJdExample example = new CategoryJdExample();
            example.createCriteria().andIdIn(idList);
          
            CategoryJd temp = new CategoryJd();
            result = CategoryJdJdService.deleteByExample(temp, example);
        } else {
            result = ResponseCode.PARAM_INVALID.getCode();
        }*/

        //删除一个
        if( StringHelper.isNotBlank(id) ){
            CategoryJd CategoryJd = new CategoryJd();
            CategoryJd.setId(id);
            result = categoryJdService.delete(CategoryJd);
        }
        
        resultMap.put(IticConst.RESULT, result);
        return resultMap;
    }
}